#include <iostream>
#include <string>
using namespace std;

//在多态中 ， 如果有子类中有成员属性开辟在堆区 ，那么父类指针在释放时无法调用子类析构函数的代码
// 解决方法：将析构函数改为虚析构和纯虚析构

//纯虚析构 改类属于抽象类 无法实现实例化对象

class Animal {
public:
    Animal() {
        cout << "Animal()的构造函数" << endl;
    }

    virtual void speak() = 0;
    //虚析构
    // virtual ~Animal() {
    //     cout << "Animal()的析构函数" << endl;
    // }
    //纯虚析构
    virtual ~Animal() = 0;
};
Animal :: ~Animal() {
        cout << "Animal()的析构函数" << endl;
    }
class Dog : public Animal {
public:
    Dog(string name) {
        m_name = new string(name);
        cout << "Dog()的构造函数" << endl;
    }
    ~Dog() {
        if (m_name != NULL) {
            delete m_name;
            m_name = NULL;
        }
        cout << "Dog()的析构函数" << endl;
    }
    void speak() {
        cout << *m_name << "狗在叫" << endl;
    }
    string * m_name;
};

int main() {
    Animal *p = new Dog("杰克");
    p->speak();
    delete p;
    return 0;
}